package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYmailOperationReq;
import com.game.server.message.proto.out.XYNotifyPartnersokRsp;
import com.game.server.message.proto.out.XYmailOperationRsp;
import com.game.server.util.ConcurrentUtil;
import net.sf.json.JSONObject;

public class XYmailOperationReq extends AbstractXYmailOperationReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            XYmailOperationRsp rsp = new XYmailOperationRsp();
            rsp.setIsok(0);
            rsp.setMid(this.mid);
            rsp.setType(this.type);

//            if (this.type == 1 || this.type == 2 || this.type == 3 || this.type == 4) {
//                XyNotificationLog log = XyNotificationLogDao.findById(this.mid);
//                if (log.getNotification_type() == 1 && log.msg_type == 1) {//俱乐部 申请加入邮件
//                    XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(log.getCid());
//                    XyClubMemberInfo clubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
//                    if (clubInfo != null && clubMemberInfo != null && clubMemberInfo.getIsManager() < 3) {//管理员  创建可操作
//                        XyClubMemberInfo toClubMemberInfo = clubInfo.getPlayer().get(log.getUid());
//                        if (toClubMemberInfo == null) {//被操作的用户不在俱乐部人员表内
//                            if (this.type == 2 && log.getState() == 0) {
//                                log.setState(2);
//                                rsp.setIsok(1);
//                                XyNotificationLogDao.updateState(log);
//                            }
//                            if (this.type == 1 && log.getState() == 0) {
//                                log.setState(1);
//                                XyClubMemberInfo newplayer = new XyClubMemberInfo();
//                                newplayer.setCid(clubInfo.getId());
//                                newplayer.setUid(log.getUid());
//                                newplayer.setIsManager(3);
//                                String additional_data = log.getAdditional_data();
//                                newplayer.setPid(0);
//                                //判断是否是通过上级合伙人搜索进入的俱乐部
//                                if (additional_data != null && !"".equals(additional_data)) {
//                                    JSONObject jsonObject = JSONObject.fromObject(additional_data);
//                                    if (jsonObject != null) {
//                                        if (jsonObject.get("searchType") != null) {
//                                            int searchType = jsonObject.getInt("searchType");
//                                            int searchid = jsonObject.getInt("searchid");
//                                            if (searchType == 2 && searchid != 0) {
//                                                XyClubMemberInfo byPartnerId = clubInfo.findByPartnerId(searchid);
//                                                if (byPartnerId != null) {
//                                                    newplayer.setPid(byPartnerId.getUid());
//                                                }
//                                            }
//                                        }
//                                    }
//                                }
//                                long clubMemberid = XyClubMemberInfoDao.insertInto(newplayer);
//                                newplayer = XyClubMemberInfoDao.findById(clubMemberid);
//                                clubInfo.addPlayer(newplayer);
//                                //将俱乐部ID添加到缓存中
//                                DataManager.getInstance().getMapUserData().get(newplayer.getUid()).getClubids().add(clubInfo.getId());
//                                rsp.setIsok(1);
//                                XyNotificationLogDao.updateState(log);
//                            }
//                        } else {
//                            if (this.type == 2 && log.getState() == 0) {
//                                log.setState(2);
//                                rsp.setIsok(1);
//                                XyNotificationLogDao.updateState(log);
//                            }
//                            if (this.type == 1 && log.getState() == 0) {
//                                if (toClubMemberInfo.getDel_type() == 1) {
//                                    log.setState(1);
//                                    int deleteClubMember = XyClubMemberInfoDao.deleteClubMember(toClubMemberInfo.getId(), 0);
//                                    if (deleteClubMember > 0) {
//                                        toClubMemberInfo.setDel_type(0);
//                                        //将俱乐部ID添加到缓存中
//                                        DataManager.getInstance().getMapUserData().get(toClubMemberInfo.getUid()).getClubids().add(clubInfo.getId());
//                                        rsp.setIsok(1);
//                                        XyNotificationLogDao.updateState(log);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//
//                if (log.getNotification_type() == 1 && log.msg_type == 2) {//俱乐部合伙人 申请邮件
//                    XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(log.getCid());
//                    XyClubMemberInfo clubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
//                    if (clubInfo != null && clubMemberInfo != null && clubMemberInfo.getIsManager() < 3) {
//                        XyClubMemberInfo toClubMemberInfo = clubInfo.getPlayer().get(log.getUid());
//                        if (this.type == 4 && log.getState() == 0) {
//                            log.setState(2);
//                            rsp.setIsok(1);
//                            XyNotificationLogDao.updateState(log);
//                        }
//                        if (this.type == 3 && log.getState() == 0) {//同意成为合伙人
//                            log.setState(1);
//                            int updateCount = XyClubMemberInfoDao.updeteIsAccessory(clubInfo.getId(), toClubMemberInfo.getUid(), 1);
//                            if (updateCount > 0) {
//                                toClubMemberInfo.setIsAccessory(1);
//                                int xyclubid = 0;
//                                while (true) {//获取一个未使用的ID
//                                    xyclubid = DataManager.getInstance().findOnlyID("xy_club_info", "id");
//                                    int count = XyClubMemberInfoDao.findPartnerIdCount(xyclubid);
//                                    if (count != 0) {
//                                        break;
//                                    }
//                                }
//                                toClubMemberInfo.setPartner_id(xyclubid);
//                                XyClubMemberInfoDao.updetePartnerId(clubInfo.getId(), toClubMemberInfo.getUid(), xyclubid);
//                                rsp.setIsok(1);
//                                XyNotificationLogDao.updateState(log);
//
//                                //同意用户成为合伙人后 如果用户还在俱乐部则 通知对方修改俱乐部权限信息
//                                LogicClient toClient = (LogicClient) LogicServer.getInstance().getClient(toClubMemberInfo.getUid());
//                                if (toClient != null && toClient.getClubid() == clubInfo.getId()) {
//                                    XYNotifyPartnersokRsp xyNotifyPartnersokRsp = new XYNotifyPartnersokRsp();
//                                    xyNotifyPartnersokRsp.setIsPartner(toClubMemberInfo.getIsAccessory());
//                                    xyNotifyPartnersokRsp.setPartnerId(toClubMemberInfo.getPartner_id());
//                                    toClient.Send(xyNotifyPartnersokRsp);
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//            if (this.type == 5 || type == 6) {
//                XyNotificationLog log = XyNotificationLogDao.findById(this.mid);
//                if (log != null && log.getState() == 0 && log.getNotification_type() == 2 && log.msg_type == 3) {
//                    XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(log.getUid());
//
//                    if (this.type == 6) {
//                        log.setState(2);
//                        rsp.setIsok(1);
//                        XyNotificationLogDao.updateState(log);
//                    }
//                    if (this.type == 5) {
//                        log.setState(1);
//                        rsp.setIsok(1);
//                        XyNotificationLogDao.updateState(log);
//                        if (xyClubInfo!=null && xyClubInfo.getUnionid()==0){
//                            XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(log.getCid());
//                            if (unionInfo!=null){
//                                XyClubInfoDao.updateUnionId(xyClubInfo.getId(),unionInfo.getId());
//                                xyClubInfo.setUnionid(unionInfo.getId());
//                                unionInfo.getClubids().add(xyClubInfo.getId());
//                            }
//                        }
//                    }
//                }
//            }

//            client.Send(rsp);
            client.Send(ConcurrentUtil.getInstance().InformMessageDispose(client,type,mid,rsp));
        }
    }

}
